<template>
  <div class="app-container">
    <el-form
      :model="queryForm"
      ref="queryForm"
      :inline="true"
      label-width="100px"
    >
      <el-form-item label="用户名称">
        <el-input size="mini" v-model="queryForm.userName" />
      </el-form-item>
      <el-form-item>
        <el-button size="mini" @click="handleGetCommonUser">搜索</el-button>
        <el-button size="mini" @click="conSignDialogVisible=true">新增</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="userCommonList">
      <el-table-column
        label="用户编号"
        align="center"
        key="userId"
        prop="userId"
      />
      <el-table-column
        label="用户名称"
        align="center"
        key="userName"
        prop="userName"
      />
      <el-table-column
        label="用户昵称"
        align="center"
        key="nickName"
        prop="nickName"
      />
      <el-table-column
        label="部门"
        align="center"
        key="deptName"
        prop="deptName"
      />
      <el-table-column
        label="手机号码"
        align="center"
        key="phonenumber"
        prop="phonenumber"
      />
      <el-table-column
        label="排序"
        align="center"
        prop="seqNum"
      >
      <template slot-scope="scope">
        <el-input size="mini" v-model="scope.row.seqNum" @change="handleSeqEdit(scope.row)"></el-input>
      </template>
      </el-table-column>
      <el-table-column
        label="任职岗位"
        align="center"
        key="hrPostStr"
        prop="hrPostStr"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

     <el-dialog
      title="选择人员"
      :visible.sync="conSignDialogVisible"
      width="50%"
    >
      <el-row :gutter="20">
        <el-col :span="12" :xs="24">
          <div class="head-container">
            <el-input
              v-model="deptName"
              placeholder="请输入部门名称"
              clearable
              size="small"
              prefix-icon="el-icon-search"
              style="margin-bottom: 20px"
            />
          </div>
          <div style="overflow-y: auto; height: 500px">
            <div class="head-container">
              <el-tree
                :data="deptOptions"
                :props="defaultProps"
                :expand-on-click-node="false"
                :filter-node-method="filterNode"
                ref="tree"
                default-expand-all
                @node-click="handleDeptNodeClick"
              />
            </div>
          </div>
        </el-col>
        <el-col :span="12" :xs="24">
          <span style="">用户名称</span>
          <el-input
            v-model="queryUserParams.nickName"
            placeholder="请输入用户名称"
            size="small"
            @change="handleUserNameChange"
          />
          <el-button size="small" @click="handleUserNameChange">搜索</el-button>
          <el-table
            :data="userList"
            max-height="500px"
            ref="userSelectTable"
            row-key="userId"
            @row-click="rowUserHandClick"
            @selection-change="handUserSelectionChange"
          >
            <el-table-column
              type="selection"
              :reserve-selection="true"
              align="center"
            />
            <el-table-column
              label="姓名"
              align="center"
              key="nickName"
              prop="nickName"
            />
            <el-table-column
              label="岗位"
              align="center"
              key="hrPostStr"
              prop="hrPostStr"
            />
          </el-table>
        </el-col>
      </el-row>
      <span slot="footer" class="dialog-footer">
        <!-- <el-button @click="uploadDialogVisible = false">取 消</el-button> -->
        <el-button type="primary" @click="handleUserSave">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
//import { listCommonUser, deleteCommonUser,addCommonUser,setUserSeq } from "@/api/contract/commonuser";

//import {
//  getUserInfo
//} from "@/api/contract/consign";
//
//import {
//  treeselectNoPre
//} from "@/api/labels";

export default {
  name: "Checkname",
  props: [""],
  data() {
    return {
      queryForm: {},
      userCommonList: [],
      userList:[],
      conSignDialogVisible:false,
      deptOptions: [],
      defaultProps: {
        children: "children",
        label: "label",
      },
      queryUserParams:{
          deptId:undefined,
          nickName:undefined
      },
      deptName: "", //筛选部门
      selectUserDataList:[]
    };
  },

  components: {},

  computed: {},

  beforeMount() {},

  mounted() {
    this.handleGetCommonUser();
    treeselectNoPre().then((res) => {
      this.deptOptions = res;
    });
  },

  methods: {
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },

    handleDelete(val) {
      let query = {
        userId: val.userId,
      };
      deleteCommonUser(query).then((response) => {
        if (response && response.code === 200) {
          this.msgSuccess("操作成功");
          this.handleGetCommonUser();
        }
      });
    },
    handleGetCommonUser() {
      listCommonUser().then((response) => {
        this.userCommonList = response.data;
      });
    },
    //选择部门
    handleDeptNodeClick(node) {
      this.queryUserParams.deptId = node.id;
      this.getUserList();
    },

        //获取人员
    getUserList() {
        getUserInfo(this.queryUserParams).then((response) => {
          this.userList = response;
        });
    },

    handleUserNameChange(){
        this.getUserList();
    },

    handleUserSave(){
        addCommonUser(this.selectUserDataList).then(response =>{
            if(response&&response.code===200){
                this.msgSuccess("新增成功！");
                this.handleGetCommonUser();
                this.conSignDialogVisible = false;
            }
        })
    },

    //用户行点击
    rowUserHandClick(row, column, event) {
      this.$refs.userSelectTable.toggleRowSelection(row);
    },

    //选择行改变
    handUserSelectionChange(selection) {
      this.selectUserDataList = selection;
    },

    //修改排序
    handleSeqEdit(row){
      setUserSeq(row).then(response =>{
        if(response&&response.code===200){
          this.msgSuccess("修改成功");
          this.handleGetCommonUser();
        }
      })
    }

  },

  watch: {},
};
</script>
<style>
</style>
